const Router = require('koa-router');
const {db, Op,QueryTypes } = require("../../models/db");
const {xe} = require("../../tools/tool");
const router = new Router();
// 换算购物车
router.post('/zd/saveCart', async ctx => {
    let usr = ctx.user
    let tenantId = usr.tenantId
    let cart = ctx.request.body.cart
    let cartModel = await db.models['cartZd'].findOne({
        where: {
            tenantId,
            buId: usr.buId,
            invId: cart.invId,
            unit: cart.unit,
        }
    })
    if(cartModel) {
        cartModel.qty = xe.add(cartModel.qty, cart.qty)
    }
    else {
        cartModel = db.models['cartZd'].build()
        cartModel.tenantId = tenantId
        cartModel.buId = usr.buId
        cartModel.invId = cart.invId
        cartModel.unit = cart.unit
        cartModel.qty = cart.qty
    }
    await cartModel.save()
    return ctx.suc('')
})

module.exports = router;
